package com.zbkj.service.service.adminregion.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.model.merchant.Merchant;
import com.zbkj.common.model.merchant.MerchantApply;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.region.SearchRegionVo;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.merchant.MerchantApplySearchRequest;
import com.zbkj.common.request.merchant.MerchantSearchRequest;
import com.zbkj.common.response.MerchantPageResponse;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.vo.DateLimitUtilVo;
import com.zbkj.service.dao.MerchantApplyDao;
import com.zbkj.service.dao.MerchantDao;
import com.zbkj.service.helper.region.RegionHelper;
import com.zbkj.service.service.adminregion.IRegionMerchantApplyService;
import com.zbkj.service.service.adminregion.IRegionMerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: RegionMerchantServiceImpl
 * @Description: TODO
 * @Author: fanbing
 * @Date: 2024/8/11 15:28
 * @Version: V1.0
 **/
@Service
@Slf4j
public class RegionMerchantApplyServiceImpl implements IRegionMerchantApplyService {

    @Autowired
    private RegionHelper regionHelper;

    @Autowired
    private MerchantApplyDao merchantApplyDao;


    @Override
    public PageInfo<MerchantApply> getRegionPage(MerchantApplySearchRequest searchRequest, PageParamRequest pageParamRequest) {
        SearchRegionVo currentUserRegion = regionHelper.getCurrentUserRegion();
        if (currentUserRegion.isAreaNullFlag()) {
            return new PageInfo<>();
        }

        Page<MerchantApply> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<MerchantApply> lqw = Wrappers.lambdaQuery();
        lqw.in(!CollectionUtils.isEmpty(currentUserRegion.getProvinceIdList()), MerchantApply::getProvinceId, currentUserRegion.getProvinceIdList());
        lqw.in(!CollectionUtils.isEmpty(currentUserRegion.getCityIdList()), MerchantApply::getCityId, currentUserRegion.getCityIdList());
        lqw.in(!CollectionUtils.isEmpty(currentUserRegion.getDistrictIdList()), MerchantApply::getDistrictId, currentUserRegion.getDistrictIdList());
        lqw.in(!CollectionUtils.isEmpty(currentUserRegion.getTownIdList()), MerchantApply::getTownId, currentUserRegion.getTownIdList());

        if (ObjectUtil.isNotNull(searchRequest.getCategoryId())) {
            lqw.eq(MerchantApply::getCategoryId, searchRequest.getCategoryId());
        }
        if (ObjectUtil.isNotNull(searchRequest.getTypeId())) {
            lqw.eq(MerchantApply::getTypeId, searchRequest.getTypeId());
        }
        if (ObjectUtil.isNotNull(searchRequest.getAuditStatus())) {
            lqw.eq(MerchantApply::getAuditStatus, searchRequest.getAuditStatus());
        }
        if (StrUtil.isNotBlank(searchRequest.getKeywords())) {
            String keywords = URLUtil.decode(searchRequest.getKeywords());
            lqw.and(i -> i.like(MerchantApply::getName, keywords).or().like(MerchantApply::getKeywords, keywords));
        }
        if (StrUtil.isNotBlank(searchRequest.getDateLimit())) {
            DateLimitUtilVo dateLimitUtilVo = CrmebDateUtil.getDateLimit(searchRequest.getDateLimit());
            lqw.between(MerchantApply::getCreateTime, dateLimitUtilVo.getStartTime(), dateLimitUtilVo.getEndTime());
        }
        lqw.orderByDesc(MerchantApply::getId);
        List<MerchantApply> applyList = merchantApplyDao.selectList(lqw);
        if (CollUtil.isEmpty(applyList)) {
            return CommonPage.copyPageInfo(page, CollUtil.newArrayList());
        }
        return CommonPage.copyPageInfo(page, applyList);
    }
}
